package top.callname.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import top.callname.bean.ConstanstBean;
import top.callname.bean.CourseInfoBean;
import top.callname.bean.StudentBean;
import top.callname.service.ClassService;
import top.callname.service.CourseService;
import top.callname.service.StudentService;
import top.callname.service.impl.ClassServiceImpl;
import top.callname.service.impl.CourseServiceImpl;
import top.callname.service.impl.StudentServiceImpl;

@WebServlet("/common")
public class CommonServlet extends HttpServlet {

	
	private Logger logger = Logger.getLogger(CommonServlet.class);
	private CourseService courseService = new CourseServiceImpl();
	private StudentService studentSrvice = new StudentServiceImpl();
	private ClassService classService = new ClassServiceImpl();
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		//response.setCharacterEncoding("UTF-8");
		//PrintWriter out = response.getWriter();
		if("course".equals(method)){ 
			String openid = request.getParameter("openid");
			
			StudentBean student =  studentSrvice.getByOpenid(openid);
			//根据班级查询课表
			if(ConstanstBean.RSP_SUCCESS.equals(student.getRspCode())){
				CourseInfoBean info = courseService.getCourseTableByCid(student.getCid());
				request.setAttribute("courseInfo", info);
				request.setAttribute("classInfo", classService.getByCid(student.getCid()));
			}else{
				logger.error("用户查询失败！");
			}
			request.getRequestDispatcher("course.jsp").forward(request,response);
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
